package com.example.d10_homework.homework07;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;

public class CjThread implements Runnable {
    private List<Integer> jackPot;

    public CjThread(List<Integer> jackPot) {
        this.jackPot = jackPot;
    }

    @Override
    public void run() {
        Random r = new Random();
        String name = Thread.currentThread().getName();
        List<Integer> temp = new ArrayList<>();
        while (true) {
            synchronized (this) {
                if (jackPot.isEmpty()) {
                    break;
                }
                int index = r.nextInt(jackPot.size());
                temp.add(jackPot.remove(index));
                // System.out.println(name + "又产生了一个" + jackPot.remove(index) + "元大奖");
            }
        }
        // 使用stream流求和
        int sum = temp.stream().mapToInt(Integer::intValue).sum();

        // 使用stream流找到最大值
        Optional<Integer> maxPrize = temp.stream().max(Integer::compare);

        // 检查最大值是否存在
        String maxPrizeStr = maxPrize.isPresent() ? maxPrize.get().toString() : "null";

        System.out.println("在此次抽奖过程中，" + name + "总共产生了" + temp.size() + "个大奖分别为" + temp +
                " 最高奖项为" + maxPrizeStr + " 总金额为" + sum + "元");
    }
}
